(function(window){

    function Player($audio){
        return new Player.prototype.init($audio);
    }
    Player.prototype = {
        constructor:Player,
        musicList:[],
        init:function($audio){
        	this.$audio = $audio;
        	this.audio = $audio.get(0);
        },
        currentIndex: -1,
        playMusic: function(index, music){
        	//判断是不是同一首音乐
        	if(this.currentIndex == index){
                // console.log('no',index)
        		if(this.audio.paused){
                    // console.log(this.musicList[index]['name']+"-->正在播放中...")
                    this.audio.play();
        			
        		}else{
        			this.audio.pause();
        		}
        	}else{
                // console.log('yes',index)
        		this.$audio.attr("src", music.url);
                // console.log(this.musicList[index]['name']+"-->正在播放中...")
                this.currentIndex =index;
        		this.audio.play()
            }
        },

        preMusic:function(){
        	var index = this.currentIndex - 1;
        	if(index < 0){
        		index = this.musicList.length - 1;
        	}
        	return index;
        },


        nextMusic:function(){
        	var index = this.currentIndex + 1;
        	if(index > this.musicList.length - 1){
        		index = 0;
        	}
        	return index;
        },
        musicTimeUpdate: function(callBack){
            var $this = this;
                        //监听播放进度
            this.$audio.on("timeupdate", function(){

                var duration = $this.audio.duration;//获取音频的实际长度
                var currentTime = $this.audio.currentTime;//获取音频的播放时刻
                var timeStr = $this.formatDate(duration, currentTime)
                callBack(duration, currentTime,timeStr);
            });
        },
        formatDate:function(duration, currentTime){
            var endMin = parseInt(duration / 60)
            var endSec = parseInt(duration % 60)
            if(endMin < 10){
                endMin = "0"+endMin;
            }
            if(endSec < 10){
                endSec = "0"+endSec;
            }
            var startMin = parseInt(currentTime / 60)
            var startSec = parseInt(currentTime % 60)
            if(startMin < 10){
                startMin = "0"+startMin;
            }
            if(startSec < 10){
                startSec = "0"+startSec;
            }
            return startMin+":"+startSec+" / "+endMin+":"+endSec;
        },

        musicSeekTo: function(value){
            if(isNaN(value)) return;
            this.audio.currentTime = this.audio.duration * value;
        },
        musicVoiceSeekTo: function(value){
            if(isNaN(value)) return;
            if(value<0 || value>1) return;
            this.audio.volume = value;
        }
    }

    Player.prototype.init.prototype = Player.prototype;
    window.Player = Player;

})(window);